import { observable, action, computed, runInAction } from 'mobx'
import { post } from 'lib/net'
import CommonVM from 'cvm/common'
import * as api from 'lib/constants/api'
import Toast from 'components/toast'

class PostItemVM {
  constructor (props) {
    this.data = props.data
    this.userInfo = props.userInfo
  }

  // 帖子详情
  @observable data = {}

  // 点赞
  @action handleLike = async () => {
    if (this.userInfo.isBlank === 1) return
    let params = {PostID: this.data.ID, UserInfoID: this.userInfo.userId}
    this.data.PointNumber += 1
    this.data.CanPoint = 0
    let res = await post(api.ADD_POINT, params)
    if (res.state === 1) {
      console.log('点赞成功')
    }
  }
}

export default class PostListVM extends CommonVM {
  constructor (props) {
    super()
    this.props = props.data
    this.api = props.api
    this.coverType = props.coverType || 'collect'
    this.fetchPosts()
  }

  // 内容数据
  @observable data = []
  // 查看更多选中item
  @observable selectedItem = {CanCollect: 1}
  // 收藏cover是否显示
  @observable coverVisible = false

  // 收藏类型
  @computed get collectType () {
    return this.selectedItem.CanCollect === 0 ? '取消收藏' : '收藏'
  }

  // 帖子详情
  @action handleComment = (item) => {
    this.props.history.push('mainText?' + item.ID)
  }

  // 请求帖子列表
  @action fetchPosts = async (resolve = null) => {
    this.fetchPage()
    let params = {
      UserInfoID: this.userId,
      Limit: this.pageInfo.size,
      Offset: this.pageInfo.page,
      timeStamp: this.pageInfo.timeStamp
    }
    let res = await post(this.api, params)
    if (res.state === 1) {
      runInAction(() => {
        if (res.data) {
          let data = res.data.map(item => {
            return new PostItemVM({data: item, userInfo: this.userInfo})
          })
          if (this.pageInfo.page === 1) {
            this.data.splice(0, this.data.length, ...data)
          } else {
            this.data.splice(this.data.length, 0, ...data)
          }
        }
        this.receivePage(res.data)
      })
    } else {
      this.receivePage(null)
    }
    if (resolve) {
      resolve()
    }
  }

  // 下拉刷新
  @action handleRefresh = async (resolve, reject) => {
    this.pageInfo.page = 1
    this.fetchPosts(resolve)
  }

  // 加载更多
  @action handleLoadMore = async () => {
    this.fetchPosts()
  }

  // 查看更多
  @action handleMore = (item) => {
    this.selectedItem = item
    this.coverVisible = true
  }

  // 删除帖子
  @action delete = async () => {
    let params = { ID: this.selectedItem.ID }
    this.handleHideCover()
    Toast.loading(1, '正在删除')
    let res = await post(api.DELETE_POST, params)
    if (res.state === 1) {
      let data = []
      this.data.forEach(item => {
        if (item.data.ID !== this.selectedItem.ID) {
          data.push(item)
        }
      })
      this.data = data
      Toast.success('删除成功')
    } else {
      Toast.fail('删除失败')
    }
  }

  // 取消收藏
  @action cancelCollect = async () => {
    let params = { PostId: this.selectedItem.PostID, UserInfoID: this.userId }
    this.handleHideCover()
    Toast.loading(1, '正在取消')
    let res = await post(api.DELETE_COLLECT, params)
    if (res.state === 1) {
      let data = []
      this.data.forEach(item => {
        if (item.data.ID !== this.selectedItem.ID) {
          data.push(item)
        }
      })
      this.data = data
      Toast.success('取消成功')
    } else {
      Toast.fail('取消失败')
    }
  }

  // 收藏帖子
  @action collect = async () => {
    let params = { PostID: this.selectedItem.ID, UserInfoID: this.userId }
    let url = ''
    let msg = ''
    if (this.selectedItem.CanCollect === 1) {
      url = api.COLLECT_POST
      this.selectedItem.CanCollect = 0
      msg = '收藏成功'
    } else {
      url = api.DELETE_COLLECT
      this.selectedItem.CanCollect = 1
      msg = '取消成功'
    }
    this.handleHideCover()
    Toast.loading(1, '正在收藏')
    let res = await post(url, params)
    if (res.state === 1) {
      Toast.success(msg)
    } else {
      Toast.fail('网络异常')
    }
  }

  // 隐藏收藏页面
  @action handleHideCover = () => {
    this.coverVisible = false
  }
}
